//
//  MXPickerView.m
//  YouOu
//
//  Created by weihong xuan on 2016/3/7.
//  Copyright © 2016年 weihong xuan. All rights reserved.
//


#define hScale ([UIScreen mainScreen].bounds.size.height) / 667
//获取屏幕 宽度、高度
#define SCREEN_WIDTH                         ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT                        ([UIScreen mainScreen].bounds.size.height)
#define ScreenWithRateFor6                   SCREEN_WIDTH/375.0
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define Color_Black_Insurance                UIColorFromRGB(0x333333)
#define Color_AddAndReduce_BorderColor       UIColorFromRGB(0xcccccc)
#define Color_me_PickerView                  UIColorFromRGB(0xf4f5f9)
#define Color_me_PickerViewTitle             UIColorFromRGB(0x333333)
#define Color_me_PickerViewFinish            UIColorFromRGB(0xe9546b)
#define Color_White                          UIColorFromRGB(0xffffff)
#define Color_Button_Bg_BLUE_NEW             UIColorFromRGB(0x2676e5)

#import "UIColor+Extensions.h"
#import "MXPickerView.h"
#import <Masonry.h>
#import "UIKitFactory.h"


@interface MXPickerView () <UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIView         *dimView;
@property (nonatomic, strong) UIButton       *btnFinish;
@property (nonatomic, copy) NSString         *strCity;
@property (nonatomic, copy) NSString         *strCityId;
@property (nonatomic, copy) NSString         *strTown;
@property (nonatomic, strong) UIPickerView   *pickerView;
@property (nonatomic, copy) NSString         *strProvinceId;
@property (nonatomic, copy) NSString         *strProvince;
@property (nonatomic, copy) NSString         *strTownId;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UIView         *titleView;
@property (nonatomic, assign) int             tmpCityIndex;
@property (nonatomic, assign) int             tmpProvinceIndex;

@end

@implementation MXPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    _arrProvince = [NSMutableArray array];
    _array = [NSMutableArray array];
 //   [_arrProvince addObjectsFromArray:[UserDefaults objectForKey:@"AddressIndexInfo"]];
    _arrResult = [NSArray array];
    _arrCity = [NSMutableArray array];
    _arrArea = [NSMutableArray array];
    self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.3];
    _dimView = [UIKitFactory createViewWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 260 * hScale) backgroundColor:Color_White superView:self];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAnimation)];
    [self addGestureRecognizer:gesture];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    _titleView = [UIKitFactory createViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) backgroundColor:Color_me_PickerView superView:_dimView];
    _btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
    [_titleView addSubview:_btnFinish];
    [_btnFinish mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(44);
    }];
    _btnFinish.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_btnFinish setTitle:@"完成" forState:UIControlStateNormal];
    [_btnFinish addTarget:self action:@selector(finishBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    [_btnFinish setTitleColor:Color_Button_Bg_BLUE_NEW forState:UIControlStateNormal];
    
    
    _lblTitle = [UIKitFactory createLabelWithFrame:CGRectZero text:nil font:[UIFont systemFontOfSize:17.0] textColor:Color_me_PickerViewTitle textAlignment:NSTextAlignmentCenter SuperView:_titleView];
    [_lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_dimView.mas_centerX).offset(0);
        make.centerY.mas_equalTo(_btnFinish.mas_centerY).offset(0);
    }];
    
    UIView *line = [UIKitFactory createViewWithFrame:CGRectZero backgroundColor:Color_White superView:_dimView];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_btnFinish.mas_bottom).offset(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(0.5);
    }];
    
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    

    [_dimView addSubview:_pickerView];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_btnFinish.mas_bottom).offset(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
}

- (void)setDataType:(DataType)dataType {
    _dataType = dataType;
    switch (dataType) {
        case Sex: {
            _lblTitle.text = @"性别";
            [_array addObjectsFromArray:@[@[@"男",@"女",@"保密"]]];
        }
            break;
        case Age: {
            _lblTitle.text = @"年龄";
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 12; i <= 140; i++) {
                [array addObject:[NSString stringWithFormat:@"%d岁",i]];
            }
            [_array addObjectsFromArray:@[array]];
        }
            break;
        case Result: {
            _lblTitle.text = @"退款原因";
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i < _arrResult.count; i++) {
                [array addObject:_arrResult[i][@"reasonInfo"]];
            }
            [_array addObjectsFromArray:@[array]];
        }
            break;
        default: {
            _lblTitle.text = @"所在地区";
        }
            break;
    }
}

#pragma mark -- UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (_dataType == Area) {
        return 3;
    } else {
        return _array.count;
    }
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (_dataType == Area) {
        if (component == 0) {
            return _arrProvince.count;
        } else if (component == 1) {
            return _arrCity.count;
        } else if (component == 2) {
            return _arrArea.count;
        }
    } else {
        NSArray *array = (NSArray *)[_array objectAtIndex:component];
        return array.count;
    }
    return 0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    if (_dataType == Area) {
        if (component == 0) {
            label.text = _arrProvince[row];
            _strProvince = label.text;
            _strProvinceId = [_arrProvince objectAtIndex:row];
        } else if (component == 1) {
            label.text = _arrCity[row];
            _strCity = label.text;
            _strCityId = [_arrCity objectAtIndex:row];
        } else if (component == 2) {
            label.text = _arrArea[row];
            _strTown = label.text;
            _strTownId = [_arrArea objectAtIndex:row];
        }
    } else {
       label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    }
    return label;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (_dataType == Area) {
        if (component == 0 && _arrProvince.count > row) {
            return _arrProvince[row];
        } else if (component == 1 && _arrCity.count > row) {
            return _arrCity[row];
            
        } else if (component == 2 && _arrArea.count > row) {
            return _arrArea[row];
            
        }
        else{
            return nil;
        }
    }else{
        return nil;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (_dataType == Area) {
        if (_reloadBlock) {
            if (component == 0 && _arrProvince.count > row) {
                _reloadBlock(component,_arrProvince[row],row);
            } else if (component == 1 && _arrCity.count > row) {
                _reloadBlock(component,_arrCity[row],row);
            } else if (component == 2 && _arrArea.count > row) {
                _reloadBlock(component,_arrArea[row],row);
            }
        }
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return SCREEN_WIDTH/3;
}

- (void)reloadData; {
    [_pickerView reloadAllComponents];
}

- (void)finishBtnEvent {
    if (_dataType == Area) {        
        if (_addressBlock) {
            _addressBlock(_strProvince,_strCity,_strTown);
        }       
    } else {
        NSString *allString = [NSString string];
        int num = 0;
        for (int i = 0; i < self.array.count; i++) {
            NSArray *arr = [self.array objectAtIndex:i];
            if (arr.count > [self.pickerView selectedRowInComponent:i]) {
                 NSString *string = [arr objectAtIndex:[self.pickerView selectedRowInComponent:i]];
                allString = [allString stringByAppendingString:string];
            }
            num = i;
        }
        if (_dataType == Result) {
            if (_resultBlock) {
                if (num<_arrResult.count) {
                _resultBlock(allString,_arrResult[num][@"reasonId"]);
                }
            }
        } else {
        if (_otherBlock) {
            _otherBlock(allString);
        }
        }
    }
    [self hideAnimation];
}

- (void)hideAnimation {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = _dimView.frame;
        frame.origin.y = SCREEN_HEIGHT;
        _dimView.frame = frame;
        self.alpha = 0.3;
        
    } completion:^(BOOL finished) {
        
        self.alpha = 0;
        [_dimView removeFromSuperview];
        [self removeFromSuperview];
        
    }];
}
- (void)showAnimation {
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = _dimView.frame;
        frame.origin.y = SCREEN_HEIGHT-260*hScale;
        _dimView.frame = frame;
    }];
}

@end
